
project(demo)

# Only generate Debug and Release configuration types.
set(CMAKE_CONFIGURATION_TYPES Debug Release)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) 
SET(CMAKE_INSTALL_RPATH "$\{ORIGIN\}")

# Use folders in the resulting project files.
set_property(GLOBAL PROPERTY OS_FOLDERS ON)

if(${CMAKE_SIZEOF_VOID_P} EQUAL 4)
	SET(BUILD_ARCH "x86")
else()
	SET(BUILD_ARCH "x64")
endif()

if(WIN32)
	set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Win/${BUILD_ARCH}")
elseif(UNIX)
	set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux")
endif()

#设置路径
if(WIN32)
	set(QT_LIB_PATH_WIN ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Win/${BUILD_ARCH}/bin)
	set(QT_PLUGINS_PATH_WIN ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Win/${BUILD_ARCH}/plugins)
else()
	set(QT_LIB_PATH_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/lib)
	set(QT_PLUGINS_PATH_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/plugins)
endif()

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if(WIN32)
	set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/output/${CMAKE_HOST_SYSTEM_NAME}/${BUILD_ARCH})
else()
	set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/output/${CMAKE_HOST_SYSTEM_NAME})
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR}/Debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR}/Release)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${OUTPUT_DIR}/Debug)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${OUTPUT_DIR}/Release)

#解压Linux上的so文件和可执行文件，以保持属性和链接
if(NOT WIN32)
	execute_process(COMMAND unzip -n ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/lib/lib.zip -d ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/lib/)
	execute_process(COMMAND unzip -n ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/bin/bin.zip -d ${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/Qt5.15/Linux/bin/)
endif()

#创建输出文件夹
if(NOT CMAKE_CONFIGURATION_TYPES)
	list(APPEND CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE})
endif()


#拷贝QT运行库
if(NOT WIN32)
	file(GLOB QT_LIBRARIES LIST_DIRECTORIES false
			${QT_LIB_PATH_LINUX}/*.*)
	list(REMOVE_ITEM QT_LIBRARIES ${QT_LIB_PATH_LINUX}/lib.zip)
	file(COPY ${QT_LIBRARIES} 
		 DESTINATION ${OUTPUT_DIR}/${CMAKE_BUILD_TYPE}
		 FOLLOW_SYMLINK_CHAIN)
endif()

foreach(config ${CMAKE_CONFIGURATION_TYPES})
	file(MAKE_DIRECTORY ${OUTPUT_DIR}/${config}/localization)
	if(WIN32)
		file(MAKE_DIRECTORY ${OUTPUT_DIR}/${config}/plugins/platforms)
	else()
		file(MAKE_DIRECTORY ${OUTPUT_DIR}/${config}/platforms)
		file(MAKE_DIRECTORY ${OUTPUT_DIR}/${config}/platforminputcontexts)
	endif()

	if(WIN32)
		if(${config} MATCHES "Release")
			set(QT_LIBRARIES
				${QT_LIB_PATH_WIN}/Qt5Core.dll
				${QT_LIB_PATH_WIN}/Qt5Gui.dll
				${QT_LIB_PATH_WIN}/Qt5Widgets.dll)

			file(COPY ${QT_PLUGINS_PATH_WIN}/platforms/qwindows.dll
			 	DESTINATION ${OUTPUT_DIR}/${config}/plugins/platforms/)
		else()
			set(QT_LIBRARIES
				${QT_LIB_PATH_WIN}/Qt5Cored.dll
				${QT_LIB_PATH_WIN}/Qt5Guid.dll
				${QT_LIB_PATH_WIN}/Qt5Widgetsd.dll)
			file(COPY ${QT_PLUGINS_PATH_WIN}/platforms/qwindowsd.dll
			 	DESTINATION ${OUTPUT_DIR}/${config}/plugins/platforms/)
		endif()
		file(COPY ${QT_LIBRARIES} 
			 DESTINATION ${OUTPUT_DIR}/${config}
			 FOLLOW_SYMLINK_CHAIN)
	else()
		file(COPY ${QT_PLUGINS_PATH_LINUX}/platforms
		 DESTINATION ${OUTPUT_DIR}/${config})
		file(COPY ${QT_PLUGINS_PATH_LINUX}/platforminputcontexts
			 DESTINATION ${OUTPUT_DIR}/${config})
	endif()
endforeach(config)

add_compile_options(-D_UNICODE -DUNICODE)
if(WIN32)
	add_compile_options(/utf-8)
endif()
add_subdirectory(source)